package ua.ad.feedsreader.actions;

import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.apache.struts2.interceptor.ParameterAware;
import org.apache.struts2.interceptor.SessionAware;

import twitter4j.TwitterException;
import ua.ad.feedsreader.mybatis.config.MyBatisSqlSessionFactory;
import ua.ad.feedsreader.mybatis.mappers.beans.ItemFeed;
import ua.ad.feedsreader.mybatis.mappers.beans.UserItemFeed;
import ua.ad.feedsreader.mybatis.mappers.beans.Utilizador;
import ua.ad.feedsreader.mybatis.mappers.clients.ItemFeedMapper;
import ua.ad.feedsreader.mybatis.mappers.clients.UserItemFeedMapper;

import com.opensymphony.xwork2.ActionSupport;
import com.restfb.DefaultFacebookClient;
import com.restfb.FacebookClient;
import com.restfb.Parameter;
import com.restfb.types.FacebookType;
import twitter4j.Status;
import twitter4j.Twitter;

public class MarkFeedAsReadAction extends ActionSupport implements SessionAware, ParameterAware {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 4037909027560292164L;
	private Map<String, Object> session;
	private Map<String, String[]> parameters;
	private String idItemFeed;
	private String share;        
	private String result;
	
	private void publishFB( SqlSession bdSession ) {
		
		ItemFeedMapper mapper = bdSession.getMapper( ItemFeedMapper.class );
		ItemFeed selectByPrimaryKey = mapper.selectByPrimaryKey(Integer.valueOf(idItemFeed));
		String chave = (String) getSession().get("chaveFb");
    	FacebookClient facebookClient = new DefaultFacebookClient( chave );
    	FacebookType publishMessageResponse = facebookClient.publish("me/feed", FacebookType.class, Parameter.with("message", selectByPrimaryKey.getTitulo() + "\n"+ selectByPrimaryKey.getUrl() ) );
    	System.out.println("Published message ID: " + publishMessageResponse.getId());
		
	}
        
        private void publishTwitter( SqlSession bdSession ) throws TwitterException {
		
		ItemFeedMapper mapper = bdSession.getMapper( ItemFeedMapper.class );
		ItemFeed selectByPrimaryKey = mapper.selectByPrimaryKey(Integer.valueOf(idItemFeed));
		String chave = (String) getSession().get("chaveFb");
                
                Twitter tw = (Twitter)getSession().get("TWITTER");
                Status status = tw.updateStatus(selectByPrimaryKey.getTitulo() +"\n"+ selectByPrimaryKey.getUrl());
                
                
	}
	
	@Override
	public String execute() throws Exception {
		
		idItemFeed = parameters.get("idItemFeed")[0];
		share = parameters.get("share")[0];
		
		Utilizador userSession = (Utilizador) getSession().get("utilizador");
        SqlSession bdSession = MyBatisSqlSessionFactory.getSqlSessionFactory().openSession();
        UserItemFeedMapper mapper = bdSession.getMapper( UserItemFeedMapper.class );
        
        UserItemFeed selectByUserAndFeed = mapper.selectByUserAndFeed( userSession.getId(), Integer.valueOf(idItemFeed) );
        if( selectByUserAndFeed != null ) {
        	
        	selectByUserAndFeed.setIdItem( Integer.valueOf( idItemFeed ) );
        	
        	if( selectByUserAndFeed.getPartilha() != 1 && Integer.valueOf( share ) == 1 ) {
        		
        		selectByUserAndFeed.setPartilha( Integer.valueOf( share ) );
        		selectByUserAndFeed.setIdTypeExtApp(1);
        		publishFB(bdSession);
        		
        	} else {
        		selectByUserAndFeed.setPartilha( 0 );
        	}
        	
        } else {
        	UserItemFeed uif = new UserItemFeed();

        	uif.setIdItem( Integer.valueOf( idItemFeed ) );
        	uif.setIdUtilizador( userSession.getId() );
        	if( Integer.valueOf( share ) == 1 ) {
        		
        		uif.setPartilha( Integer.valueOf( share ) );
        		uif.setIdTypeExtApp(1);
        		
        		publishFB(bdSession);
                }else if(Integer.valueOf(share)==2){
                    uif.setPartilha( Integer.valueOf( share ) );
        		uif.setIdTypeExtApp(1);
                        publishTwitter(bdSession);
                        
        	} else {
        		uif.setPartilha( 0 );
        	}
        	
        	mapper.insertSelective( uif );
        }
        
        bdSession.commit();
        bdSession.close();
        
        
        result = SUCCESS;
        
		return SUCCESS;
	}

	/**
	 * @param session the session to set
	 */
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
	
	public Map<String, Object> getSession() {
		return session;
	}
	/**
	 * @return the idItemFeed
	 */
	public String getIdItemFeed() {
		return idItemFeed;
	}

	/**
	 * @param idItemFeed the idItemFeed to set
	 */
	public void setIdItemFeed(String idItemFeed) {
		this.idItemFeed = idItemFeed;
	}

	/**
	 * @return the share
	 */
	public String getShare() {
		return share;
	}

	/**
	 * @param share the share to set
	 */
	public void setShare(String share) {
		this.share = share;
	}

	@Override
	public void setParameters(Map<String, String[]> parameters) {
		this.parameters = parameters;
	}

	/**
	 * @return the result
	 */
	public String getResult() {
		return result;
	}

	/**
	 * @param result the result to set
	 */
	public void setResult(String result) {
		this.result = result;
	}
}
